package com.dragon.modules.yuegou.service.impl.company;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.busd.DistrictEntity;
import com.dragon.modules.yuegou.entity.company.CompanyEntity;
import com.dragon.modules.yuegou.enums.code.BusinessCode;
import com.dragon.modules.yuegou.mapper.company.CompanyMapper;
import com.dragon.modules.yuegou.model.busd.District;
import com.dragon.modules.yuegou.model.company.Company;
import com.dragon.modules.yuegou.query.company.CompanyQuery;
import com.dragon.modules.yuegou.service.busd.DistrictService;
import com.dragon.modules.yuegou.service.company.CompanyService;
import com.dragon.modules.yuegou.utils.PrivateStringUtils;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:40
 * @version 2.0
 */
@Service("compCompanyService")
public class CompanyServiceImpl
		extends ServiceImpl<CompanyMapper, CompanyEntity>
		implements CompanyService {

	@Autowired
	private DistrictService districtService;
	
	@Override
	public IPage<Company> search(PageRequest page, CompanyQuery query) {
		IPage<Company> iPage = this.companyList(page,query);
		for (Company company : iPage.getRecords()) {
			if (!StringUtils.isEmpty(company.getBusdId())) {
				District district = districtService.getDistrictById(company.getBusdId());
				company.setBcName(district.getBcName());
			}
		}
		return iPage;
	}
	

	@Override
	public void saveCompany(Company company) {
		Check.notNull(company.getName(), "单位名称不能为空");
		Check.length(company.getName(), 0, 20, "单位名称不能超过二十字");
		Check.notEmpty(company.getTaxpayerIdentificationNumber(),"纳税人识别号不能为空");
		Check.notNull(company.getContactName(), "联系人名称不能为空");
		Check.length(company.getContactName(), 0, 10, "联系人名称不能超过十字");
		Check.notNull(company.getContactMobile(), "联系人手机号不能为空");
		Check.notNull(company.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());
		if (!PrivateStringUtils.isMobileNO(company.getContactMobile())) {
			throw new DragonException(BusinessCode.PHONE_WRONG_FORMAT);
		}
		CompanyQuery companyQuery = new CompanyQuery();
		companyQuery.setName(company.getName());
		companyQuery.setDel(false);
		if (company!=null && StringUtils.isEmpty(company.getId())) {
			//判断单位名是否存在
			
			Long count = count(companyQuery.toQueryWrapper());
			if (count>0) {
				throw new DragonException(BusinessCode.UNIT_NAME_EXISTS);
			}
			companyQuery.setName(null);
			companyQuery.setTaxpayerIdentificationNumber(company.getTaxpayerIdentificationNumber());
			Long count2 = count(companyQuery.toQueryWrapper());
			if (count2>0) {
				throw new DragonException(BusinessCode.TAXPAYER_NAME_EXISTS);
			}
			//插入企业信息
			save(company);
		}else {
			//判断单位名是否存在
			List<CompanyEntity> list = list(companyQuery.toQueryWrapper());
			if (list.size()>0 && !list.get(0).getId().equals(company.getId())) {
				throw new DragonException(BusinessCode.UNIT_NAME_EXISTS);
			}
			companyQuery.setName(null);
			companyQuery.setTaxpayerIdentificationNumber(company.getTaxpayerIdentificationNumber());
			List<CompanyEntity> list2 = list(companyQuery.toQueryWrapper());
			if (list2.size()>0 && !list2.get(0).getId().equals(company.getId())) {
				throw new DragonException(BusinessCode.TAXPAYER_NAME_EXISTS);
			}
			//修改企业信息
			baseMapper.updateById(company);
		}
	}

	@Override
	public Company getCompany(String companyId) {
		CompanyEntity entity = getById(companyId);
		DistrictEntity district = districtService.getById(entity.getBusdId());

		Company company = new Company(entity);
		company.setBcName(district.getBcName());
		return company;
	}

	@Override
	public List<Company> findCompany(String companyName) {
		CompanyQuery companyQuery = new CompanyQuery();
		if (!StringUtils.isEmpty(companyName)) {
			companyQuery.name().like(companyName);
		}
		companyQuery.setDel(false);
		List<CompanyEntity> entities = baseMapper.selectList(companyQuery.toQueryWrapper());
		return entities.stream().map(Company::new).collect(Collectors.toList());
	}

	@Override
	public IPage<Company> companyList(PageRequest page, CompanyQuery query) {
		IPage<CompanyEntity> iPage = new Page<>(page.getPage(),page.getSize(),page.getCount());

		IPage<CompanyEntity> entityIPage = baseMapper.selectPage(iPage,query.toQueryWrapper());
		return entityIPage.convert(Company::new);
	}

}